<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="60%" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="top">
			<el-form-item label="发帖主题" prop="title">
				<el-input v-model="form.title" placeholder="发帖主题" maxlength="48" clearable></el-input>
			</el-form-item>
			<sc-editor v-model="form.content" placeholder="请输入" :height="400" clearable  style="width: 100%;"></sc-editor>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">发 帖</el-button>
		</template>
	</el-dialog>
</template>

<script>
import {defineAsyncComponent} from "vue";
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	components: {
		scEditor
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '发帖对话框',
				edit: '编辑对话框',
				reply: '回帖对话框',
			},
			form: {
				recordId:"",
				discussId: "",
				title: "",
				content: "",
			},
			rules: {
				title: [
					{required: true, message: '发帖主题不能为空'}
				],
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if (this.mode == "add") {
						this.isSaveing = true;
						res = await this.$API.info.discuss.insertDiscussRecord.post(this.form);
						this.isSaveing = false;
					} else if (this.mode == "reply") {
						this.isSaveing = true;
						res = await this.$API.info.discuss.revDiscussRecord.post(this.form);
						this.isSaveing = false;
					}else if (this.mode == "edit") {
						this.isSaveing = true;
						res = await this.$API.info.discuss.updateDiscussRecord.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			if(this.mode=="mode")
			{
				this.form.discussId = data.discussId
			}else
			{
				this.form.discussId = data.discussId
				this.form.recordId=data.recordId;
				this.form.title =data.title
				this.form.content = data.content
			}
		}
	}
}
</script>

<style>
</style>
